/*
 * Unmqr.cs
 * 
 * Copyright (c) 2003-2004, dnAnalytics Project. All rights reserved.
*/

#if !MANAGED
using System;
using System.Runtime.InteropServices;

using dnA.Utility;

namespace dnA.Math.Lapack{
	[System.Security.SuppressUnmanagedCodeSecurityAttribute]
	internal sealed class Unmqr {
		private  Unmqr() {}                           
		private static void ArgumentCheck(Side side, int m, int n, int k, Object A, int lda, Object tau, Object C, int ldc) {
			if ( A == null ) {
				throw new ArgumentNullException("A","A cannot be null.");
			}
			if ( tau == null ) {
				throw new ArgumentNullException("tau","tau cannot be null.");
			}
			if ( C == null ) {
				throw new ArgumentNullException("C","C cannot be null.");
			}
			if ( m<0 ) {
				throw new ArgumentException("m must be at least zero.");
			}
			if ( n<0 ) {
				throw new ArgumentException("n must be at least zero.");
			}
			if( side == Side.Left ){
				if( k < 0 || k > m ){
					throw new ArgumentException("k must be positive and less than or equal to m.");
				}
			}else{
				if( k < 0 || k > n ){
					throw new ArgumentException("k must be positive and less than or equal to n.");
				}
			}
			if( side == Side.Left ){
				if ( lda < System.Math.Max(1,m) ) {
					throw new ArgumentException("lda must be at least max(1,m)");
				}
			}else{
				if ( lda < System.Math.Max(1,n) ) {
					throw new ArgumentException("lda must be at least max(1,n)");
				}
			}
			if ( ldc < System.Math.Max(1,m) ) {
				throw new ArgumentException("ldc must be at least max(1,m)");
			}			
		}

		internal static int Compute( Side side, Transpose trans, int m, int n, int k, ComplexFloat[] A, int lda, ComplexFloat[] tau, ComplexFloat[] C, int ldc  ){
			ArgumentCheck(side, m, n, k, A, lda, tau, C, ldc);
			return dna_lapack_cunmqr(Configuration.BlockSize, side, trans, m, n, k, A, lda, tau, C, ldc);
		}

		internal static int Compute( Side side, Transpose trans, int m, int n, int k, ComplexDouble[] A, int lda, ComplexDouble[] tau, ComplexDouble[] C, int ldc  ){
			ArgumentCheck(side, m, n, k, A, lda, tau, C, ldc);
			return dna_lapack_zunmqr(Configuration.BlockSize, side, trans, m, n, k, A, lda, tau, C, ldc);
		}

		[DllImport(dnA.Utility.Configuration.BLASLibrary, ExactSpelling=true, SetLastError=false)]
		private static extern int dna_lapack_cunmqr( int block_size, Side side, Transpose trans, int m, int n, int k, [In,Out]ComplexFloat[] A, int lda, [In,Out]ComplexFloat[] tau, [In,Out]ComplexFloat[] C, int ldc   );

		[DllImport(dnA.Utility.Configuration.BLASLibrary, ExactSpelling=true, SetLastError=false)]
		private static extern int dna_lapack_zunmqr( int block_size, Side side, Transpose trans, int m, int n, int k, [In,Out]ComplexDouble[] A, int lda, [In,Out]ComplexDouble[] tau, [In,Out]ComplexDouble[] C, int ldc   );
	}
}
#endif